package com.woshua.writer;

import com.woshua.annotation.ViewInjector;
import com.woshua.utils.FileUtils;

import java.io.IOException;
import java.io.Writer;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.VariableElement;

public class DefaultAdapterWriter extends AbstractWriter {

    public DefaultAdapterWriter(ProcessingEnvironment processingEnvironment) {
        super(processingEnvironment);
    }

    @Override
    protected void generateImport(Writer writer, InjectorInfo info) throws IOException {
        writer.write("package " + info.packageName + " ;");
        writer.write("\n\n");
        writer.write("import com.woshua.knifelibrary.adapter.InjectAdapter;");
        writer.write("\n");
        writer.write("import com.woshua.knifelibrary.adapter.ViewFinder;");

        writer.write("\n\n\n");
        writer.write("/* This class is generated by Simple ViewInjector, please don't modify! */ ");
        writer.write("\n");
        FileUtils.print("public class " + info.newClassName
                + " implements InjectAdapter<" + info.className + "> { ");
        writer.write("public class " + info.newClassName
                + " implements InjectAdapter<" + info.className + "> { ");
        writer.write("\n");
        writer.write("\n");
        // 查找方法
        writer.write("  public void injects(" + info.className
                + " target)  { ");
        writer.write("\n");
    }

    @Override
    protected void writeField(Writer writer, VariableElement element, InjectorInfo injectInfo) throws IOException {
        ViewInjector injector = element.getAnnotation(ViewInjector.class);
        //  writer.write(" ViewFinder.setContentView(target,"  + injector.value() + "  ) ; \n");
        String fieldName = element.getSimpleName().toString();
        writer.write("      target." + fieldName + " =  ViewFinder.findViewById(target, "
                + injector.value() + "  ) ; ");
        writer.write("\n");
    }

    @Override
    protected void writeEnd(Writer writer) throws IOException {
        writer.write("  }");
        writer.write("\n\n");
        writer.write(" } ");
    }
}
